﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Kugar.Core.BaseStruct;
using Kugar.Core.WinForm;
using Kugar.Core.ExtMethod;
using TreeNode = System.Windows.Forms.TreeNode;

namespace Kugar.UI.WinForm
{
    public partial class ControlListEditor : Form
    {
        private ControlListEditor()
        {
            InitializeComponent();
        }

        public ControlListEditor(IEnumerable<Control> controlList,Control sourceControl):this(controlList,sourceControl,true){}

        public ControlListEditor(IEnumerable<Control> controlList,Control sourceControl,bool isContransSelf):this()
        {
            ControlList = controlList;

            SourceControl = sourceControl;

            IsContransSelf = isContransSelf;
                
            var srcFrm = sourceControl.FindForm();

            if(srcFrm==null)
            {
                return;
            }

            GetChildControl(null,srcFrm);

            if(controlList!=null)
            {
                foreach (var control in controlList)
                {
                    var node = treeView1.Nodes.Find(control.Name, true);

                    if (node.Length>0)
                    {
                        node.Update(s => s.Checked = true);
                    }
                }                
            }
        }

        public IEnumerable<Control> ControlList { private set; get; }

        public Control[] Value
        {
            get 
            {
                var lst = new List<Control>();

                var checkedLst = treeView1.FindNode(p => p.Checked);

                foreach (var item in checkedLst)
                {
                    lst.Add((Control)item.Tag);
                }

                return lst.ToArray();
            }
        }

        public Control SourceControl { private set; get; }

        public bool IsContransSelf { private set; get; }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void GetChildControl(TreeNode parentNode,Control parentControl)
        {
            if (!parentControl.HasChildren)
            {
                return;
            }
            else
            {
                for (int i = 0; i < parentControl.Controls.Count; i++)
                {
                    var ctrl = parentControl.Controls[i];

                    if (ctrl == SourceControl && !IsContransSelf)
                    {
                        continue;
                    }

                    if (ctrl.Name.IsEmptyOrWhileSpace())
                    {
                        continue;
                    }

                    var node = new TreeNode(ctrl.Name);

                    node.Name = ctrl.Name;
                    node.Tag = ctrl;

                    if (parentNode==null)
                    {
                        treeView1.Nodes.Add(node);
                    }
                    else
                    {
                        parentNode.Nodes.Add(node);
                    }
                    
                    GetChildControl(node,ctrl);
                }
            }

        }

    }
}
